using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using ReflectionStudio.Core.Events;
using System;

namespace WPFExcelReport
{
    public partial class VWGBST
    {
        public VWASTAMM VWS
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VWS"))
                {
                    ENVMAIN["VWS"] = new VWASTAMM();
                }
                return (VWASTAMM)ENVMAIN["VWS"];
            }
        }
        public VWAFARBEN VWAF
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VWAF"))
                {
                    ENVMAIN["VWAF"] = new VWAFARBEN();
                }
                return (VWAFARBEN)ENVMAIN["VWAF"];
            }
        }
        public VWBDAT VWB
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VWB"))
                {
                    ENVMAIN["VWB"] = new VWBDAT();
                }
                return (VWBDAT)ENVMAIN["VWB"];
            }
        }
        public VWBBST VWBB
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VWBB"))
                {
                    ENVMAIN["VWBB"] = new VWBBST();
                }
                return (VWBBST)ENVMAIN["VWBB"];
            }
        } 
       
        public static readonly VWGBST SWG = new VWGBST();
        public DataRow dwWG = null; 
        public void WBBS10(MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGME10"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGME10"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTA"].ToString());
            dwWG["WGBSTA"] = current + increased;
            dwWG["WGBSTME"] = dwWG["WGBSTA"];

            current = Convert.ToDouble(dwWG["WGME10KG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGME10KG"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTAKG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGBSTAKG"] = current + increased;
            Update(dwWG);
        }

        public void WBBS20( MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGME20"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGME20"] = current + increased;
            current = Convert.ToDouble(dwWG["WGME20KG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGME20KG"] = current + increased;
            Update(dwWG);
        }

        public void WBBS21(MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGME21"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGME21"] = current + increased;
            current = Convert.ToDouble(dwWG["WGME21KG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGME21KG"] = current + increased;
            Update(dwWG);
        }

        public void WBBS42( MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGME42"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGME42"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTA"].ToString());
            dwWG["WGBSTA"] = current - increased;
            dwWG["WGBSTME"] = dwWG["WGBSTA"];
            current = Convert.ToDouble(dwWG["WGME42KG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGME42KG"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTAKG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGBSTAKG"] = current - increased;
            Update(dwWG);
        }

        public void WBBS43(MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGME43"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGME43"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTA"].ToString());
            dwWG["WGBSTA"] = current + increased;
            dwWG["WGBSTME"] = dwWG["WGBSTA"];
            current = Convert.ToDouble(dwWG["WGME43KG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGME43KG"] = current + increased;
            current = Convert.ToDouble(dwWG["WGBSTAKG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGBSTAKG"] = current + increased;
            Update(dwWG);
        }

        public void WBBS61( MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGMEZUTEI"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGMEZUTEI"] = current + increased;
            current = Convert.ToDouble(dwWG["WGMEZUTEIKG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGMEZUTEIKG"] = current + increased;
            Update(dwWG);
        }

        public void WBBS62(MessageInfo msg)
        {
            LOADWG(msg);
            if (dwWG == null) return;
            double current = 0;
            double increased = 0;
            current = Convert.ToDouble(dwWG["WGMEZUTEI"].ToString());
            increased = Convert.ToDouble(msg.P1) / 10;
            dwWG["WGMEZUTEI"] = current - increased;
            current = Convert.ToDouble(dwWG["WGMEZUTEIKG"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            dwWG["WGMEZUTEIKG"] = current - increased;
            Update(dwWG);
        }

        public void Update(DataRow dwWG)
        {
            dwWG["WGAENB"] = app.User;
            dwWG["WGAEND"] = date;
            UpdateWGBST(dwWG, app.company, 0);
        }

        public DataSet LOADWG(MessageInfo msg)
        { 
            if (TBMain.Rows.Count > 0)
            {
                string filter = "WGWAKZ='" + msg.WAKZ + "' AND WGART='" + msg.ART + "' AND WGDESS='" + msg.DESS + "' AND WGCOLORIT='" + msg.COLORIT + "' AND WGBESCHICH='" + msg.BESCHICH 
                    + "' AND WGFARBE='"+msg.FARBE+"' AND WGVAR='" + msg.VAR + "' AND WGZUSTAND='" + msg.ZUSTAND + "' AND WGZENSURKZ='" + msg.ZENSURKZ+"' AND WGLO='" +msg.LO+"' AND WGMJJ='"+year+"' AND  WGMM='"+month+"'" ;
                DataRow[] rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwWG = row;
                    return SetProd;
                }
            }
            dwData = TBMain.NewRow();
            dwData["WGMANDANT"] = app.Mandant;
            dwData["WGSA"] = "00";
            dwData["WGKUNDNR"] = "00000000";
            dwData["WGWERK"] = "   ";
            dwData["WGABT"] = "   ";
            dwData["WGWAKZ"] = msg.WAKZ;
            dwData["WGART"] = msg.ART;
            dwData["WGDESS"] = msg.DESS;
            dwData["WGCOLORIT"] = msg.COLORIT;
            dwData["WGFARBE"] = msg.FARBE;
            dwData["WGBESCHICH"] = msg.BESCHICH;
            dwData["WGVAR"] = msg.VAR;
            dwData["WGBESCHICH"] = msg.BESCHICH.PadRight(10);
            dwData["WGDESS"] = msg.DESS.PadRight(10);
            dwData["WGCOLORIT"] = msg.COLORIT.PadRight(10);
            dwData["WGZUSTAND"] = msg.ZUSTAND; ;
            dwData["WGZENSUR"] = msg.ZENSUR;
            dwData["WGZENSURKZ"] = msg.ZENSURKZ;
            dwData["WGLO"] = msg.LO;
            dwData["WGMJJ"] = year;
            dwData["WGMM"] = month;
            SResult = QWG02(dwData);
            MergeData(SResult, false, false);
            if (TBMain.Rows.Count == 0) 
            {

                if (!RefreshByWB14(msg))
                {
                    NewByWB14(msg);
                }
            }
            dwWG = TBMain.Rows[0];
            return SetProd;
        } 
        
        public void NewByWB14(MessageInfo msg)
        {
            dwData = TBMain.NewRow();
             SetUpNewRow(ref dwData);
            dwData["WGMANDANT"] = app.Mandant;
            dwData["WGMANDANT"] = app.Mandant;
            dwData["WGSA"] = "00";
            dwData["WGWAKZ"] = msg.WAKZ;
            dwData["WGART"] = msg.ART;
            dwData["WGDESS"] = msg.DESS;
            dwData["WGCOLORIT"] = msg.COLORIT;
            dwData["WGFARBE"] = msg.FARBE;
            dwData["WGBESCHICH"] = msg.BESCHICH;
            dwData["WGVAR"] = msg.VAR;
            dwData["WGDESS"] = msg.DESS.PadRight(10);
            dwData["WGCOLORIT"] = msg.COLORIT.PadRight(10);
            dwData["WGZUSTAND"] = msg.ZUSTAND; ;
            dwData["WGZENSUR"] = msg.ZENSUR;
            dwData["WGZENSURKZ"] = msg.ZENSURKZ;
            dwData["WGLO"] = msg.LO;
            dwData["WGMJJ"] = year;
            dwData["WGMM"] = month;
           
            DataRow dwWS = VWS.TBMain.NewRow();
            dwWS["WAMANDANT"] = app.Mandant;
            dwWS["WAART"] = msg.ART;
            dwWS["WAVAR"] = msg.VAR;
            SResult = VWS.QWS01(dwWS);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwData["WGBRT"] = row["WAFBRTU"];
                dwData["WGGRQM"] = row["WAFGRQM"];
            } 
            InsertWGBST(dwData);
            TBMain.Rows.InsertAt(dwData, 0);
        }
   
        public bool RefreshByWB14(MessageInfo msg)
        {
            bool Has = false;
            dwData = TBMain.NewRow();
            dwData["WGMANDANT"] = app.Mandant;
            string LastMonth = "";
            string begin = "";
            string end = "";
            Helper.GetLastMonth(ref LastMonth, ref begin, ref end);
            //dwData["WGAEND"]=WGMJJ+WGMM;
            dwData["WGMJJ"] = LastMonth.Substring(0, 4);
            dwData["WGMM"] = LastMonth.Substring(4, 2);
            dwData["WGLO"] = msg.LO;
            dwData["WGWAKZ"] = msg.WAKZ;
            dwData["WGART"] = msg.ART;
            dwData["WGVAR"] = msg.VAR;
            dwData["WGFARBE"] = msg.FARBE;
            dwData["WGBESCHICH"] = msg.BESCHICH.PadRight(10);
            dwData["WGDESS"] = msg.DESS.PadRight(10);
            dwData["WGCOLORIT"] = msg.COLORIT.PadRight(10);
            SResult = QWG05(dwData);
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwData.ItemArray = SResult.Tables[0].Rows[0].ItemArray;
                #region
                dwData["WGMM"] = month;
                dwData["WGME01"] = "0";
                dwData["WGME01KG"] = "0";
                dwData["WGME10"] = "0";
                dwData["WGME10KG"] = "0";
                dwData["WGME11"] = "0";
                dwData["WGME11KG"] = "0";
                dwData["WGME12"] = "0";
                dwData["WGME12KG"] = "0";
                dwData["WGME13"] = "0";
                dwData["WGME13KG"] = "0";
                dwData["WGME14"] = "0";
                dwData["WGME14KG"] = "0";
                dwData["WGME15"] = "0";
                dwData["WGME15KG"] = "0";
                dwData["WGME19"] = "0";
                dwData["WGME19KG"] = "0";
                dwData["WGME20"] = "0";
                dwData["WGME20KG"] = "0";
                dwData["WGME21"] = "0";
                dwData["WGME21KG"] = "0";
                dwData["WGME31"] = "0";
                dwData["WGME31KG"] = "0";
                dwData["WGME32"] = "0";
                dwData["WGME32KG"] = "0";
                dwData["WGME33"] = "0";
                dwData["WGME33KG"] = "0";
                dwData["WGME34"] = "0";
                dwData["WGME34KG"] = "0";
                dwData["WGME35"] = "0";
                dwData["WGME35KG"] = "0";
                dwData["WGME36"] = "0";
                dwData["WGME36KG"] = "0";
                dwData["WGME37"] = "0";
                dwData["WGME37KG"] = "0";
                dwData["WGME40"] = "0";
                dwData["WGME40KG"] = "0";
                dwData["WGME41"] = "0";
                dwData["WGME41KG"] = "0";
                dwData["WGME42"] = "0";
                dwData["WGME42KG"] = "0";
                dwData["WGME43"] = "0";
                dwData["WGME43KG"] = "0";
                dwData["WGME46"] = "0";
                dwData["WGME46KG"] = "0";
                dwData["WGME47"] = "0";
                dwData["WGME47KG"] = "0";
                dwData["WGME48"] = "0";
                dwData["WGME48KG"] = "0";
                dwData["WGME49"] = "0";
                dwData["WGME49KG"] = "0";
                dwData["WGME50"] = "0";
                dwData["WGME50KG"] = "0";
                dwData["WGME51"] = "0";
                dwData["WGME51KG"] = "0";
                dwData["WGME52"] = "0";
                dwData["WGME52KG"] = "0";
                dwData["WGME53"] = "0";
                dwData["WGME53KG"] = "0";
                dwData["WGME55"] = "0";
                dwData["WGME55KG"] = "0";
                dwData["WGME73"] = "0";
                dwData["WGME73KG"] = "0";
                dwData["WGME76"] = "0";
                dwData["WGME76KG"] = "0";
                dwData["WGME79"] = "0";
                dwData["WGME79KG"] = "0";
                dwData["WGME80"] = "0";
                dwData["WGME88"] = "0";
                dwData["WGME89"] = "0";
                dwData["WGME90"] = "0";
                dwData["WGME90KG"] = "0";
                dwData["WGME91"] = "0";
                dwData["WGME91KG"] = "0";
                dwData["WGMER04"] = "0";
                dwData["WGMER05"] = "0";
                dwData["WGRESV1"] = "0";
                dwData["WGRESV2"] = "0";
                dwData["WGRESV3"] = "0";
                dwData["WGRESV4"] = "0";
                dwData["WGRESV5"] = "0";
                #endregion 
                InsertWGBST(dwData, app.company);
                TBMain.Rows.InsertAt(dwData, 0); 
                Has = true;
            }
            return Has;
        }
    }
     
}
